<?php
/**
 * Created by PhpStorm.
 * User: xiaomage
 * Date: 2020/12/22
 * Time: 14:56
 */

namespace app\system\middleware;


use app\system\logic\User;
use common\Variable;
use component\system\system_action\SystemAction;
use think\Request;

class ApiAction
{
    /**
     * @param Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        // controller/action 形式
        $path_info = $request->pathinfo();
        if (preg_match('/\//', $path_info)) {
            $controller = $request->controller();
            $action = $request->action();
        } else {
            // system/xxx_xx 下划线形式
            $controller = (new Variable())->transToPascal($path_info);
            $action = '';
        }

        SystemAction::instance()->save(
            [
                'user_id' => User::uuid(),
                'path' => $path_info,
                'method' => strtolower($request->method()),
                'controller' => $controller,
                'action' => $action,
                'params' => json_encode($request->param())
            ]
        );

        return $next($request);
    }
}